#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

#include <errno.h>
#include <string.h>

#include <iostream>
#include <string>

#include "BasicUsageEnvironment.hh"

#include "MediaSink.hh"

#include "DelayQueue.hh"
#include "RTSPClient.hh"

using namespace std;

RTSPClient* ourRTSPClient = NULL;
Authenticator* auth = NULL;

void sendSetupCB(RTSPClient *rtspclient,int resultCode,char* resultStr){
	cout<<"call sendDescribeCB resultCode:"<<resultCode<<" resultStr:"<<resultStr<<endl;
	if(resultCode == 0){
		;//ourRTSPClient->sendSetupCommand();
	}
	delete[] resultStr;
}

void sendDescribeCB(RTSPClient *rtspclient,int resultCode,char* resultStr){
	cout<<"call sendDescribeCB resultCode:"<<resultCode<<" resultStr:"<<resultStr<<endl;
	if(resultCode == 0){
		;//ourRTSPClient->sendSetupCommand();
	}else if (resultCode == 401){
		ourRTSPClient->sendDescribeCommand(sendDescribeCB,auth);
	}
	delete[] resultStr;
}

void sendOptionCB(RTSPClient *rtspclient,int resultCode,char* resultStr){
	cout<<"call sendOptionCB resultCode:"<<resultCode<<" resultStr:"<<resultStr<<endl;
	if(resultCode == 0){
		ourRTSPClient->sendDescribeCommand(sendDescribeCB,NULL);
	}
	delete[] resultStr;
}

int main(int argc,char **argv){
	cout<<"this live555 test app"<<endl;
	TaskScheduler* scheduler = BasicTaskScheduler::createNew();
	BasicUsageEnvironment *env = BasicUsageEnvironment::createNew(*scheduler);

	char* url = "rtsp://192.168.9.29";
	ourRTSPClient = RTSPClient::createNew(*env,url,0);
	auth = new Authenticator("admin","12345");
	ourRTSPClient->sendOptionsCommand(sendOptionCB,NULL);
	env->taskScheduler().doEventLoop();
	return 0;
}
